<script>

    $("#nav-change").children().click(function () {
        $(this).addClass('active');
        $(this).siblings().removeClass('active');
        var title = $(this).children().html();
        if (title == '执行命令') {
            $("#div-cmd").show();
            $("#div-file").hide();
            $("#div-script").hide();
        } else if (title == '上传文件') {
            $("#div-cmd").hide();
            $("#div-file").show();
            $("#div-script").hide();
        } else {
            $("#div-cmd").hide();
            $("#div-file").hide();
            $("#div-script").show();
        }
    });

    var setting = {
        view: {
            selectedMulti: false,
        },
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        edit: {
            enable: false
        }
    };

    var zNodes = {{ .ztree_data}};
    $(document).ready(function () {
        treeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    });

    //ip地址正则匹配
    function isValidIP(ip) {
        var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
        return reg.test(ip);
    }

    function getIpList() {
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = treeObj.getCheckedNodes(true);
        var ip_list = "";
        for (var i = 0; i < nodes.length; i++) {
            var ip_name = nodes[i].name;
            ip = ip_name.split("_")[0];
            if (isValidIP(ip)) {
                ip_list += ip + ",";
            }
        }
        return ip_list
    }

    //批量执行命令
    $("#sub-cmd").click(function () {
        $("#txt-cmd").text("命令执行中，请耐心等待！");
        var ip_list = getIpList();
        var cmd = $("#exec-cmd").val();
        if (ip_list != "") {
            if (cmd) {
                $.ajax({
                    type: "post",
                    async: false,
                    url: "/ops/cmd/",
                    data: {"ip_list": ip_list, "cmd": cmd},
                    dataType: "json",
                    success: function (data) {
                        $("#txt-cmd").text(data);
                    },
                    error: function (data) {
                        spop({
                            template: data.responseText,
                            style: 'error',
                            autoclose: 2000
                        });
                    }
                });
            } else {
                var msg = "请输入命令！"
            }
        } else {
            var msg = '请选择服务器！'
        }
        if (msg) {
            spop({
                template: msg,
                style: 'warning',
                autoclose: 2000
            });
        }
    });


    //批量执行脚本
    $("#sub-script").click(function () {
        $("#txt-script").text("脚本执行中，请耐心等待！");
        var ip_list = getIpList();
        var formData = new FormData();
        var script_file = $("#script-file").get(0).files[0];
        var size = script_file.size;
        var filesize = (size / 1024).toFixed(2);
        var maxSize = 1024;//文件上传大小限制
        if (filesize > maxSize) {
            msg = "上传脚本文件不许大于" + maxSize + "KB"
        } else {
            if (ip_list != "") {
                if (script_file) {
                    formData.append("ip_list", ip_list);
                    formData.append("script_file", script_file);
                    $.ajax({
                        type: "post",
                        processData: false,
                        contentType: false,
                        url: "/ops/script/",
                        data: formData,
                        success: function (data) {
                            $("#txt-script").text(data);
                        },
                        error: function (data) {
                            spop({
                                template: data.responseText,
                                style: 'error',
                                autoclose: 2000
                            });
                        }
                    });
                } else {
                    var msg = "请选择脚本！"
                }
            } else {
                var msg = '请选择服务器！'
            }
        }
        if (msg) {
            spop({
                template: msg,
                style: 'warning',
                autoclose: 2000
            });
        }
    });


    //上传文件
    $("#sub-upload-file").click(function () {
        $("#txt-file").text("文件上传中，请耐心等待！");
        var ip_list = getIpList();
        var to_dir = $("#upload-dir").val();
        var upload_file = $("#upload-file").get(0).files[0];
        var formData = new FormData();

        var size = upload_file.size;
        var filesize = (size / 1024).toFixed(2);
        var maxSize = 1024;//文件上传大小限制
        if (filesize > maxSize) {
            msg = "上传脚本文件不许大于" + maxSize + "KB"
        } else {
            if (ip_list != "") {
                if (upload_file) {
                    formData.append("ip_list", ip_list);
                    formData.append("to_dir", to_dir);
                    formData.append("upload_file", upload_file);
                    $.ajax({
                        type: "post",
                        processData: false,
                        contentType: false,
                        url: "/ops/upfile/",
                        data: formData,
                        success: function (data) {
                            $("#txt-file").text(data);
                        },
                        error: function (data) {
                            spop({
                                template: data.responseText,
                                style: 'error',
                                autoclose: 2000
                            });
                        }
                    });
                } else {
                    var msg = "请选择上传文件"
                }
            } else {
                var msg = '请选择服务器！'
            }
        }
        if (msg) {
            spop({
                template: msg,
                style: 'warning',
                autoclose: 2000
            });
        }
    });


    $("#exec-cmd").on('input', function () {
        var key = $(this).val();
        $.ajax({
            type: "post",
            async: false,
            url: "/ops/cmdkey/",
            data: {"key": key},
            dataType: "json",
            success: function (data) {
                var availableTags = data;
                $("#exec-cmd").autocomplete({
                    source: availableTags
                });
            },
            error: function (data) {
                spop({
                    template: data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        });
    });

</script>